package com.algorithm;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: sun.hongliang
 * @create: 2019/07/22 10:44
 */
public class LetterCombinationsTest {

    private Map<Character, String> map = new HashMap<Character, String>() {
        private static final long serialVersionUID = -1726055983652856953L;

        {
            put('2', "abc");
            put('3', "def");
            put('4', "ghi");
            put('5', "jkl");
            put('6', "mno");
            put('7', "pqrs");
            put('8', "tuv");
            put('9', "wxyz");
        }
    };

    private List<String> result = new ArrayList<>();

    public List<String> letterCombinations(String digits) {
        if (digits != null && digits.length() != 0) {
            comb("", digits);
        }
        return result;
    }

    private void comb(String content, String nextDigits) {
        if (nextDigits == null || nextDigits.length() == 0) {
            result.add(content);
        } else {
            char nextChar = nextDigits.charAt(0);
            String nextChars = map.get(nextChar);
            int length = nextChars.length();
            String substring = nextDigits.substring(1);
            for (int i = 0; i < length; i++) {
                comb(content + nextChars.substring(i, i + 1), substring);
            }
        }
    }

    public static void main(String[] args) {
        LetterCombinationsTest test = new LetterCombinationsTest();
        List<String> strings = test.letterCombinations("23");
        System.out.println(strings);
    }

}
